package net.xo68.destiny.manager.mapper;


import net.xo68.destiny.manager.entity.Trigger;
import org.apache.ibatis.annotations.*;
import java.util.List;

/** 
 * Trigger mybatis mapper
 * @author wuxie(16349023@qq.com)
 * @date 2020年12月21日 21:43:52
 *
 */
public interface TriggerMapper {


	
	@Select("SELECT * FROM destiny_trigger WHERE job_id = #{jobId}")
    @Results(value = { 
    @Result(id = true, column = "job_id", property = "jobId"),
        @Result(column = "prev_fire_time", property = "prevFireTime"),
        @Result(column = "next_fire_time", property = "nextFireTime"),
        @Result(column = "job_state", property = "jobState"),
        @Result(column = "trigger_type", property = "triggerType"),
        @Result(column = "start_time", property = "startTime"),
        @Result(column = "end_time", property = "endTime")
    })
    Trigger find(@Param("jobId") Long jobId);
	
	@Select("SELECT * FROM destiny_trigger")
    @Results(value = { 
    @Result(id = true, column = "job_id", property = "jobId"),
        @Result(column = "prev_fire_time", property = "prevFireTime"),
        @Result(column = "next_fire_time", property = "nextFireTime"),
        @Result(column = "job_state", property = "jobState"),
        @Result(column = "trigger_type", property = "triggerType"),
        @Result(column = "start_time", property = "startTime"),
        @Result(column = "end_time", property = "endTime")
    })
    List<Trigger> list();

    @Select("SELECT * FROM destiny_trigger" +
            " where job_state=1 and (next_fire_time < DATE_ADD(NOW(),INTERVAL #{bufferTimeSecond} SECOND) or next_fire_time is NULL)" +
            " and (start_time <= NOW() or start_time is NULL)" +
            " and (end_time > NOW() or end_time is NULL)" +
            " limit #{triggerObserverMaxSize}")
    @Results(value = {
            @Result(id = true, column = "job_id", property = "jobId"),
            @Result(column = "prev_fire_time", property = "prevFireTime"),
            @Result(column = "next_fire_time", property = "nextFireTime"),
            @Result(column = "job_state", property = "jobState"),
            @Result(column = "trigger_type", property = "triggerType"),
            @Result(column = "start_time", property = "startTime"),
            @Result(column = "end_time", property = "endTime")
    })
    List<Trigger> waitObservelist(@Param("bufferTimeSecond") long bufferTimeSecond, @Param("triggerObserverMaxSize") int triggerObserverMaxSize);
	
	@Insert("insert into destiny_trigger (job_id ,prev_fire_time ,next_fire_time ,trigger_state ,trigger_type ,start_time ,end_time ) values (#{jobId} , #{prevFireTime} , #{nextFireTime} , #{triggerState} , #{triggerType} , #{startTime} , #{endTime}  )")
    @Options(useGeneratedKeys = true, keyProperty = "jobId")
    int insert(Trigger trigger);
	
	@Update({"update destiny_trigger set",
      " prev_fire_time=#{prevFireTime}, next_fire_time=#{nextFireTime}, trigger_state=#{triggerState}, trigger_type=#{triggerType}, start_time=#{startTime}, end_time=#{endTime} ",
      "where job_id = #{jobId}"
    })
    int update(Trigger trigger);

    /**
     * 更新触发时间
     * @param triggers
     * @return
     */
    @Update({"<script>" +
            "<foreach item='trigger' index='index' collection='triggers' open='' separator=';' close=''>" +
            "update destiny_trigger set prev_fire_time=#{trigger.prevFireTime}, next_fire_time=#{trigger.nextFireTime}" +
            " where job_id = #{trigger.jobId}" +
            "</foreach>" +
            "</script>"
    })
    int updateFireTime(@Param("triggers")List<Trigger> triggers);

	@Delete("delete FROM destiny_trigger WHERE job_id = #{jobId}")
	int delete(@Param("jobId") Long jobId);
}